package com.fh.controller.ccode.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fh.controller.base.BaseController;
import com.fh.service.ccode.HomeService;
import com.fh.util.AppUtil;
import com.fh.util.PageData;

@Controller
@RequestMapping(value = "/apphome")
public class HomeController extends BaseController {
	@Resource(name = "homeService")
	private HomeService homeService;

	/**
	 * 首页轮播图片
	 */
	@RequestMapping(value = "/pictures")
	@ResponseBody
	public Object getPictures(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");

		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();

		try {

			List<PageData> list = homeService.getAllPictures(pd);
			List<String> tempList = new ArrayList<String>();
			for (int i = 0; i < list.size(); i++) {
				String url = list.get(i).getString("PIC_URL").toString();
				tempList.add(url);
			}

			map.put("pictures", tempList);
			map.put("code", "T");
			map.put("msg", "获取成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	/**
	 * 获取高校数、人才数、方案数
	 */
	@RequestMapping(value = "/getAllNum")
	@ResponseBody
	public Object getAllNums(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, String> map = new HashMap<String, String>();
		PageData pd = new PageData();
		pd = this.getPageData();

		try {

			pd = homeService.getAllNums(pd);
			map.put("code", "T");
			map.put("msg", "获取成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取失败");
		}

		return AppUtil.returnObject(new PageData(), pd);
	}

	/**
	 * 获取当前任务数、RMB总悬赏数
	 */
	@RequestMapping(value = "/missions")
	@ResponseBody
	public Object getMissionNumAndAward(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");

		Map<String, String> map = new HashMap<String, String>();
		PageData pd = new PageData();
		pd = this.getPageData();

		try {
			pd = homeService.getMissionNumAndAward(pd);
			map.put("code", "T");
			map.put("msg", "获取成功");

		} catch (Exception e)
		{
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取失败");
		}

		return AppUtil.returnObject(new PageData(), pd);
	}
}
